<template>
  <div class="app-container">
    <h2 style="text-align: center;">上传数据集</h2>
    <el-form class="upload">
      <el-form-item>
        <el-upload
          ref="upload"
          :on-change="fileChange"
          :auto-upload="false"
          :on-success="fileUploadSuccess"
          :on-remove="handleRemove"
          :file-list="fileList"
          :on-error="fileUploadError"
          :disabled="importBtnDisabled"
          :limit="2"
          :action="BASE_API+'/file/upload'"
          :multiple="false"
          :headers="myHeaders"
          class="upload-demo"
          drag
          accept="text/csv"
        >
          <i class="el-icon-upload"/>
          <div class="el-upload-text-text">将文件拖到此处,只能上传csv文件</div>
          <div class="el-upload-text-text-btn">或 <em>点击上传</em></div>
          <el-button
            v-if="fileList.length>0"
            :loading="loading"
            style="margin-left: 10px;"
            size="small"
            type="success"
            @click.stop.prevent
            @click="submitUpload">上传到服务器</el-button>
        </el-upload>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { getToken } from '@/utils/auth'
export default {
  data() {
    return {
      fileList: [],
      myHeaders: { token: getToken() },
      BASE_API: process.env.BASE_API, // 接口API地址
      importBtnDisabled: false, // 按钮是否禁用,
      loading: false,
      dialogImageUrl: '',
      dialogVisible: false,
      disabled: false
    }
  },
  methods: {
    submitUpload() {
      this.importBtnDisabled = true
      this.loading = true
      this.$refs.upload.submit()
    },

    // 上传成功
    fileUploadSuccess(response) {
      this.loading = false
      this.$message({
        type: 'success',
        message: '上传成功'
      })
      this.$router.push({ path: '/analyze/history' })
    },
    // 上传失败
    fileUploadError() {
      this.loading = false
      this.$message({
        type: 'error',
        message: '上传文件失败'
      })
    },
    handleRemove(file) {
      this.fileList.pop()
    },
    fileChange(file, fileList) {
      this.fileList.pop()
      this.fileList.push(file)
    }
  }
}
</script>

<style lang="scss">
form.upload{
  width: 100% !important;
  .el-upload{
  width: 100%;

  .el-upload-dragger{
    height: unset;
    width: 100%;
    .el-upload-text-text{
      color: #000 !important;
    }
  }
  .el-upload-list{
    display: flex;
    justify-content: center;
    align-items: center;
      a{
        display: flex !important;
        flex-direction: column !important;
        align-items: center !important;
        width: fit-content !important;
    }
  }
}
}

</style>

